package com.android.webframework;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Environment;
import android.util.Log;

public class WFLog {

	private File logLocation = new File(Environment.getExternalStorageDirectory(), "WebFrameWorkLog.txt");
	private String logTag;
	
	/**
	 * Default WLog Constructor
	 */
	public WFLog(){
		this.logTag = "WebServices";
	}
	
	public void setLogTag(String logTag){
		this.logTag = logTag;
	}
	/**
	 * Verify the log
	 */
	protected void verifyLogging(){
		if (!logLocation.exists()){
			try{
				logLocation.createNewFile();
			}
			catch(Exception e){
				e.printStackTrace();
				/*PAOLO DEAL WITH THIS */
			}
		}
	}
	
	/**
	 * Append test and class tag to the log
	 * @param text the text to be added
	 * @param classTag the tag of the class
	 */
	protected void appendToLog(String text){
		verifyLogging();
		try{
			BufferedWriter buffer = new BufferedWriter(new FileWriter(logLocation, true));
			//Generate appropriate date and time for current log
			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			Date date = new Date();
			buffer.append(this.logTag + " " + dateFormat.format(date) + " " + text + "\n");
			buffer.newLine();
			buffer.flush();
			buffer.close();
		}
		catch(Exception e){
			Log.e(this.logTag,"Write to Log File Failure:" + e.getMessage());
		}
	}
}
